package com.lzj.test3;

import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class TrackerService extends Service {

    public static final String COMMAND = "COMMAND";
    public static final String COMMAND_OPEN = "COMMAND_OPEN";
    public static final String COMMAND_CLOSE = "COMMAND_CLOSE";
    TrackerWindowManager mTrackerWindowManager;

    private MyHandler myHandler;

    @Override
    public void onCreate() {
        super.onCreate();
        myHandler=new MyHandler();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (true){
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//
//                    myHandler.sendEmptyMessage(1);
//                }
//            }
//        }).start();
    }

    private void initTrackerWindowManager(){
        if (mTrackerWindowManager==null){
            mTrackerWindowManager=new TrackerWindowManager(TrackerService.this);
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        initTrackerWindowManager();

        String command=intent.getStringExtra(COMMAND);
        if (command!=null){
            if (command.equals(COMMAND_OPEN)){
                mTrackerWindowManager.addView();
            }else {
                mTrackerWindowManager.removeView();
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    class MyHandler extends Handler{
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            Log.e("FloatingView", "handleMessage: -----------"+isHome());
            if (isHome()){
                mTrackerWindowManager.addView();
            }else {
                mTrackerWindowManager.removeView();
            }
        }
    }

    /**
     * 推断当前界面是否是桌面
     */
    private boolean isHome() {
        ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> rti = mActivityManager.getRunningTasks(1);
        return getHomes().contains(rti.get(0).topActivity.getPackageName());
    }

    /**
     * 获得属于桌面的应用的应用包名称
     *
     * @return 返回包括全部包名的字符串列表
     */
    private List<String> getHomes() {
        List<String> names = new ArrayList<String>();
        PackageManager packageManager = this.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo ri : resolveInfo) {
            names.add(ri.activityInfo.packageName);
        }
        return names;
    }

}
